package com.warski.wonderfires;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Window;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.warski.wonderfires.dlna.DLNAManager;
import com.warski.wonderfires.dlna.listener.DLNAControlListener;
import com.warski.wonderfires.dlna.player.DLNAPlayer;
import com.warski.wonderfires.utils.FileUtils;

import org.fourthline.cling.model.action.ActionInvocation;

import java.util.Arrays;

import static android.media.AudioManager.STREAM_RING;

/**
 * Created by xuhaiping on 2020/8/29.
 */
public class MainActivity extends AppCompatActivity {

    public static MainActivity self;
    public static MainActivity getInstance(){
        return self;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.getSupportActionBar().hide();
        self = this;
        // 要申请的权限
        String[] permissions = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.INTERNET,
                Manifest.permission.WRITE_SECURE_SETTINGS, Manifest.permission.RECORD_AUDIO,Manifest.permission.READ_EXTERNAL_STORAGE};
        // 版本判断。当手机系统大于 23 时，才有必要去判断权限是否获取
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // 检查该权限是否已经获取
            int i = ContextCompat.checkSelfPermission(getApplicationContext(), permissions[0]);
            int l = ContextCompat.checkSelfPermission(getApplicationContext(), permissions[1]);
            int m = ContextCompat.checkSelfPermission(getApplicationContext(), permissions[2]);
            int n = ContextCompat.checkSelfPermission(getApplicationContext(), permissions[3]);
            int o = ContextCompat.checkSelfPermission(getApplicationContext(), permissions[4]);
            Log.d("Observable","Observable");
            // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
            int granted = PackageManager.PERMISSION_GRANTED;
            if (i != granted && l != granted && m != granted && n != granted && o != granted) {
                ActivityCompat.requestPermissions(this, permissions, 123);
            }
        }
        Window window = getWindow();
        DisplayMetrics metric = new DisplayMetrics();
        window.getWindowManager().getDefaultDisplay().getMetrics(metric);
        // 屏幕宽度（像素）
        TheApplication.width = metric.widthPixels;
        // 屏幕高度（像素）
        TheApplication.height = metric.heightPixels;
        TheApplication.mActivity = this;
        TheApplication.mContext = this;
        initUtils();
        startDLNA();

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 123) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                    //如果没有获取权限，那么可以提示用户去设置界面--->应用权限开启权限
                    Toast.makeText(getApplicationContext(),"Please go to set the interface opening permission",Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                if (DLNAManager.dlnaPlayer != null){
                    DLNAManager.dlnaPlayer.getVolume(null,false);
                    return true;
                }
            case KeyEvent.KEYCODE_VOLUME_UP:
                if (DLNAManager.dlnaPlayer != null){
                    DLNAManager.dlnaPlayer.getVolume(null,true);
                    return true;
                }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        BottomNavigationView navView = findViewById(R.id.nav_view);
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_normal, R.id.navigation_auto, R.id.navigation_led, R.id.navigation_setting)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
    }

    @Override
    protected void onDestroy() {
        stopDLNA();
        super.onDestroy();
    }

    /**
     * 重启当前Activity
     */
    public void restartAct() {
        finish();
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        //清除Activity退出和进入的动画
        overridePendingTransition(0, 0);
    }

    /**
     * 初始化一些工具类
     */
    private void initUtils() {
        FileUtils.init(this.getApplicationContext());
    }

    /**
     * 初始化dlna
     */
    private void startDLNA() {
        DLNAManager.init(this.getApplicationContext());
    }

    /**
     * 初始化dlna
     */
    private void stopDLNA() {
        DLNAManager.release();
    }
}
